package com.zyzd.baseprojectofvolly.utils;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import java.text.DecimalFormat;

/**
 * Created by 李宗源 on 2016/8/13.
 */
public class StringUtils {
    private static DecimalFormat decimalFormat;

    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(CharSequence input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    public static DecimalFormat getDecimalFormat() {
        if (decimalFormat == null) {
            decimalFormat = new DecimalFormat(".00");
        }
        return decimalFormat;
    }

    public static String formatFloatRetainsOne(double number) {
        return getDecimalFormat().format(number);
    }

    public static String formatPeopleNums(int people) {
        if (people < 10000) {
            return String.format("%d", people);
        } else {
            return String.format("%.2f 万", people / 10000f);
        }
    }

    public static SpannableStringBuilder indentText(String text) {
        SpannableStringBuilder span = new SpannableStringBuilder("缩进" + text);
        span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return span;
    }

}
